<?php
namespace app\common\model;
use traits\model\SoftDelete;

class AuthRule extends Base
{
    use SoftDelete;
    protected $deleteTime = 'delete_time'; // 软删除字段
    protected $autoWriteTimestamp = true;

    /** 获取面包屑 */
    public function getMbx($behavior)
    {
        $rule = $this->field('name,title,parent_id')->where(['name'=>$behavior])->find();
        if($rule){
            if($rule['parent_id'] > 0){
                $rule_p = $this->field('name,title')->where(['id'=>$rule['parent_id']])->find();
                $rule['rule_p'] = $rule_p;
            }
        }

        return $rule;
    }

    /** 获取父级路由 */
    public function getActive($behavior)
    {
        $rule = $this->field('name,title,parent_id')->where(['name'=>$behavior])->find();
        $active = [];
        if($rule){
            $active = $this->_getActive($rule);
        }else{
            $active['active'] = '';
            $active['active2'] = '';
        }

        return $active;
    }

    public function _getActive($rule)
    {
        $name = [];
        if($rule['parent_id'] > 0){
            $rule_p = $this->field('name,title,parent_id')->where(['id'=>$rule['parent_id']])->find();
            $name['active'] = $rule_p['name'];
            $name['active2'] = '';
            // 3级菜单
            if($rule_p){
                if($rule_p['parent_id'] > 0){
                    $rule_p2 = $this->field('name,title,parent_id')->where(['id'=>$rule_p['parent_id']])->find();
                    $name['active'] = $rule_p2['name'];
                    $name['active2'] = $rule_p['name'];
                }
            }
        }
        return $name;

    }

    /** 获取菜单栏 */
    public function getMenu($rules)
    {

        $menu_0 = $this->_getMenu($rules,0,0);

        $menu = $this->getMenu2($rules,$menu_0);
        return $menu;
    }


    public function getMenu2($rules,$menu_0)
    {

        foreach ($menu_0 as $k0=>$v0){

            $menu_1 = $this->_getMenu($rules,$v0['level']+1,$v0['id']);

            if(!empty($menu_1)){
                $menu_0[$k0]['menu_p'] = $menu_1;
                $this->getMenu2($rules,$menu_1);
            }

        }

        return $menu_0;
    }

    public function _getMenu($rules,$level,$pid)
    {
        if($rules == '*'){
            $where = [];
        }else{
            $rules = explode(',',$rules);
            $where['id'] = ['in',($rules)];
        }

        $menu = $this->field('*')->where($where)->where(['ismenu'=>1,'level'=>$level,'parent_id'=>$pid])->order('listorder desc')->select();

        return $menu;

    }

    public function getMenuAdmin($rules)
    {

        $menu_0 = $this->_getMenu2($rules,0,0);

        $menu = $this->getMenu3($rules,$menu_0);
        return $menu;
    }

    public function getMenu3($rules,$menu_0)
    {

        foreach ($menu_0 as $k0=>$v0){

            $menu_1 = $this->_getMenu2($rules,$v0['level']+1,$v0['id']);

            if(!empty($menu_1)){
                $menu_0[$k0]['menu_p'] = $menu_1;
                $this->getMenu3($rules,$menu_1);
            }

        }

        return $menu_0;
    }


    public function _getMenu2($rules,$level,$pid)
    {
        if($rules == '*'){
            $where = [];
        }else{
            $rules = explode(',',$rules);
            $where['id'] = ['in',($rules)];
        }

        $menu = $this->field('*')->where($where)->where(['ismenu'=>1,'status'=>1,'level'=>$level,'parent_id'=>$pid])->order('listorder desc')->select();

        return $menu;

    }

}